﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using MakeIT.MiraPark.Negocios;

namespace MiraPark
{
    public partial class FormCadastroParametro : Form
    {
        private string operacao = "";

        public FormCadastroParametro()
        {
            InitializeComponent();
        }

        private void FormCadastroParametro_Load(object sender, EventArgs e)
        {
            //Popular a lista de parametros cadastrados
            this.dataGridViewParametros.DataSource = ModelBO.getParametros();
            setupButtonsControls();
        }

        private void dataGridViewParametro_SelectionChanged(object sender, EventArgs e)
        {
            setupButtonsControls();
        }

        private void buttonNovo_Click(object sender, EventArgs e)
        {
            this.operacao = "novo";
            setupButtonsControls();
        }

        private void buttonCancelar_Click(object sender, EventArgs e)
        {
            this.operacao = "cancelar";
            setupButtonsControls();
        }

        private void buttonEditar_Click(object sender, EventArgs e)
        {
            this.operacao = "editar";
            setupButtonsControls();
        }

        private void buttonExcluir_Click(object sender, EventArgs e)
        {
            this.operacao = "excluir";

            List<ParametroVO> parametroList = (List<ParametroVO>)this.dataGridViewParametros.DataSource;
            ParametroVO parametro = parametroList[this.dataGridViewParametros.CurrentRow.Index];
            DialogResult dr = MessageBox.Show(this, "Confirma a exclusão do parametro \r\n\t" + parametro.idParametro, "Confirmação", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr.ToString().Equals("Yes"))
            {
                ModelBO.delParametro(parametro);

                int nRow = this.dataGridViewParametros.CurrentRow.Index;
                this.dataGridViewParametros.DataSource = ModelBO.getParametros();
                if (this.dataGridViewParametros.Rows.Count > 0)
                {
                    dataGridViewParametros.CurrentCell = dataGridViewParametros.Rows[Math.Min(nRow, this.dataGridViewParametros.Rows.Count - 1)].Cells[0];
                    dataGridViewParametros.Rows[Math.Min(nRow, this.dataGridViewParametros.Rows.Count - 1)].Selected = true;
                }

                setupButtonsControls();
            }
        }

        private void buttonSalvar_Click(object sender, EventArgs e)
        {
            String erros = "";
            if (this.textBoxId.Text.Trim().Length == 0)
                erros = erros + "ID do Parâmetro deve ser informado.\r\n";
            if (this.textBoxParametro.Text.Trim().Length == 0)
                erros = erros + "Valor deve ser informado.\r\n";
            if (erros.Length > 0)
            {
                MessageBox.Show(this, erros, "Aviso", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ParametroVO parametro = null;

            if (this.operacao.Equals("novo"))
            {

                parametro = ParametroVO.CreateParametroVO();
                parametro.idParametro = this.textBoxId.Text;
                parametro.parametro = this.textBoxParametro.Text;
                parametro.criptografado = this.checkBoxCript.Checked;

                ModelBO.newParametro(parametro);
            }
            if (this.operacao.Equals("editar"))
            {
                List<ParametroVO> parametroList = (List<ParametroVO>)this.dataGridViewParametros.DataSource;
                parametro = parametroList[this.dataGridViewParametros.CurrentRow.Index];

                if (this.checkBoxCript.Checked && (bool)this.checkBoxCript.Tag && 
                  ((string)this.textBoxParametro.Tag).Equals(this.textBoxParametro.Text))
                      parametro.parametro = ModelBO.Decrypt(parametro.parametro);
                else
                    parametro.parametro = this.textBoxParametro.Text;
                parametro.criptografado = this.checkBoxCript.Checked;

                ModelBO.setParametro(parametro);
            }
            this.operacao = "salvar";

            List<ParametroVO> listParametroVOs = ModelBO.getParametros();
            dataGridViewParametros.DataSource = listParametroVOs;
            int nRow = listParametroVOs.FindIndex(a => a.idParametro == parametro.idParametro);
            dataGridViewParametros.CurrentCell = dataGridViewParametros.Rows[nRow].Cells[0];
            dataGridViewParametros.Rows[nRow].Selected = true;

            setupButtonsControls();
        }


        private void setupButtonsControls()
        {
            bool haRows = this.dataGridViewParametros.Rows.Count > 0 && this.dataGridViewParametros.CurrentRow!=null;
            bool novo = this.operacao.Equals("novo");
            bool edit = this.operacao.Equals("editar");
            bool canc = this.operacao.Equals("cancelar");

            this.buttonCancelar.Enabled = novo || edit;
            this.buttonNovo.Enabled = !novo && !edit;
            this.buttonEditar.Enabled = haRows && !novo && !edit;
            this.buttonExcluir.Enabled = haRows && !novo && !edit;
            this.buttonSalvar.Enabled = novo || edit;

            this.textBoxId.Enabled = novo;
            this.textBoxParametro.Enabled = novo || edit;
            this.checkBoxCript.Enabled = novo || edit;

            this.dataGridViewParametros.Enabled = !novo && !edit;

            if (!haRows || novo)
            {
                this.textBoxId.Text = "";
                this.textBoxParametro.Text = "";
                this.textBoxParametro.Tag = null;
                this.textBoxParametro.UseSystemPasswordChar = false;
                this.checkBoxCript.Checked = false;
                this.checkBoxCript.Tag = null;
            }
            else {
                List<ParametroVO> parametroList = (List<ParametroVO>)this.dataGridViewParametros.DataSource;
                ParametroVO parametro = parametroList[this.dataGridViewParametros.CurrentRow.Index];
                this.textBoxId.Text = parametro.idParametro.ToString();
                this.textBoxParametro.Text = parametro.parametro;
                this.textBoxParametro.Tag = parametro.parametro;
                this.checkBoxCript.Checked = parametro.criptografado;
                this.checkBoxCript.Tag = parametro.criptografado;
                this.textBoxParametro.UseSystemPasswordChar = parametro.criptografado;
            }
            if (novo)
                this.textBoxId.Focus();
            if (edit)
                this.textBoxParametro.Focus();
        }

        private void checkBoxCript_CheckedChanged(object sender, EventArgs e)
        {
            this.textBoxParametro.UseSystemPasswordChar = this.checkBoxCript.Checked;
        }

     }
}
